package com.lwl.controller;

import com.lwl.entity.LayData;
import com.lwl.entity.Tuser;
import com.lwl.service.ILoginService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author Lei
 * @create 2022-10-16 11:39
 */

@Controller
public class LoginController {

    @Resource
    private ILoginService iLoginService;

    /**
     *     登录时验证登录用户的信息是否符合条件，根据用户的权限进行分配左侧菜单目录
     *     ，并把登录用户的信息存到session中方便后面使用
     * @return
     */
    @PostMapping("loginTest")
    @ResponseBody
    public LayData loginTest(HttpSession session, Tuser tuser){
        Tuser tuser1 = iLoginService.loginTest(tuser);
        LayData layData = new LayData();
        if (tuser1!=null){
            layData.setData(true);
            session.setAttribute("user",tuser1);
            String username = tuser.getUsername();
            session.setAttribute("loginUsername",username);
        }
        return layData;
    }

    /**
     * 主菜单的退出时要删除session的内容，避免相同的访问路径还能再次进入系统
     * @param session
     * @return
     */
    @PostMapping("deleteSession")
    @ResponseBody
    public LayData deleteSession (HttpSession session){
        session.invalidate();
        return  new LayData(true);
    }
}
